package com.bidianying.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;


public class BaseController{
	
	public int getRequestParamsInt(HttpServletRequest req, String paramsName,int defaultVal) {
		int intparam = defaultVal;
		String val = req.getParameter(paramsName);
		if (StringUtils.isBlank(val)||val.equals("null"))
			return intparam;
		try {
			intparam = Integer.parseInt(val);
		} catch (Exception e) {
			e.printStackTrace();
			return defaultVal;
		}
		return intparam;

	}
	
	public String getRequestParamstr(HttpServletRequest req, String paramsName) {
		String val = "";
		try {
			val = StringUtils.isNotBlank(req.getParameter(paramsName)) ? req
					.getParameter(paramsName) : "";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return val;

	}
	
	public String getReqIp(HttpServletRequest request) {
		if (request == null)
			return "";
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.trim().length() == 0
				|| "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
		}
		if (ip == null || ip.trim().length() == 0
				|| "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.trim().length() == 0
				|| "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.trim().length() == 0
				|| "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip != null && ip.trim().length() > 15) {
			ip = ip.trim().split(",")[0];
		}
		return ip;
	}
	
	
}